package com.strange.mdl.module.mdl.service.dailydharma;

import cn.hutool.core.collection.CollUtil;
import com.strange.mdl.module.mdl.controller.admin.dailydharma.vo.DailydharmaUsersPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.dailydharma.vo.DailydharmaUsersSaveReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.dailydharma.DailydharmaUsersDO;
import com.strange.mdl.module.mdl.dal.mysql.dailydharma.DailydharmaUsersMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.util.object.BeanUtils;


import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.convertList;
import static com.strange.mdl.framework.common.util.collection.CollectionUtils.diffList;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.*;

/**
 * 今日弘法用户操作 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class DailydharmaUsersServiceImpl implements DailydharmaUsersService {

    @Resource
    private DailydharmaUsersMapper dailydharmaUsersMapper;

    @Override
    public void createDailydharmaUsers(DailydharmaUsersSaveReqVO createReqVO) {
        // 插入
        DailydharmaUsersDO dailydharmaUsers = BeanUtils.toBean(createReqVO, DailydharmaUsersDO.class);
        dailydharmaUsersMapper.insert(dailydharmaUsers);
    }

    @Override
    public void updateDailydharmaUsers(DailydharmaUsersSaveReqVO updateReqVO) {
        // 校验存在
        validateDailydharmaUsersExists(updateReqVO.getDailydharmaId());
        // 更新
        DailydharmaUsersDO updateObj = BeanUtils.toBean(updateReqVO, DailydharmaUsersDO.class);
        dailydharmaUsersMapper.updateById(updateObj);
    }

    @Override
    public void deleteDailydharmaUsers(Long id) {
        // 校验存在
        validateDailydharmaUsersExists(id);
        // 删除
        dailydharmaUsersMapper.deleteById(id);
    }

    @Override
        public void deleteDailydharmaUsersListByIds(List<Long> ids) {
        // 校验存在
        validateDailydharmaUsersExists(ids);
        // 删除
        dailydharmaUsersMapper.deleteByIds(ids);
        }

    private void validateDailydharmaUsersExists(List<Long> ids) {
        List<DailydharmaUsersDO> list = dailydharmaUsersMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DAILYDHARMA_USERS_NOT_EXISTS);
        }
    }

    private void validateDailydharmaUsersExists(Long id) {
        if (dailydharmaUsersMapper.selectById(id) == null) {
            throw exception(DAILYDHARMA_USERS_NOT_EXISTS);
        }
    }

    @Override
    public DailydharmaUsersDO getDailydharmaUsers(Long id) {
        return dailydharmaUsersMapper.selectById(id);
    }

    @Override
    public PageResult<DailydharmaUsersDO> getDailydharmaUsersPage(DailydharmaUsersPageReqVO pageReqVO) {
        return dailydharmaUsersMapper.selectPage(pageReqVO);
    }

}